package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
public class OrderTask {
    @Autowired
    private OrdersMapper ordersMapper;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void fuckOrder() {
        LocalDateTime time = LocalDateTime.now().minusMinutes(15);
        List<Orders> ordersList = ordersMapper.getfuck(Orders.PENDING_PAYMENT, time);

        if (ordersList != null && ordersList.size() > 0) {
            log.info("修该订单状态....start");
            ordersList.forEach(orders -> {
                orders.setStatus(Orders.CANCELLED);
                orders.setCancelTime(LocalDateTime.now());
                orders.setCancelReason("超时未支付");
                ordersMapper.update(orders);
            });
            log.info("修该订单状态....end");
        }
    }

    //@Scheduled(cron = "0 10 11 * * ?") 测试
    @Scheduled(cron = "0 0 1 * * ?")
    public void mdfOrder() {
        LocalDateTime time = LocalDateTime.now().minusHours(1);
        List<Orders> ordersList = ordersMapper.getfuck(Orders.DELIVERY_IN_PROGRESS, time);

        if (ordersList != null && ordersList.size() > 0) {
            log.info("修该订单状态....start");
            ordersList.forEach(orders -> {
                orders.setStatus(Orders.CANCELLED);
                ordersMapper.update(orders);
            });
            log.info("修该订单状态....end");
        }
    }
}
